package kr.saeildamil.androidforstudy2.ch28;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import kr.saeildamil.androidforstudy2.R;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

public class PullParserActivity extends Activity {
	ListView list;
	ProgressDialog mProgress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch28_xmlparser);
		
		list = (ListView) findViewById(R.id.ch28_xmlparser_listview);
		
		new ParsingXml().execute("order.xml");
	}
	
	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgress = ProgressDialog.show(PullParserActivity.this, "Wait", "Now Making order.xml") ;
		}

		@Override
		protected ArrayList<Item> doInBackground(String... params) {
			StringBuilder sb = new StringBuilder();
			String str = "";
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(params[0])));
				while ((str = br.readLine())!= null) {
					sb.append(str);
				}
				br.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}
		
		@Override
		protected void onPostExecute(ArrayList<Item> result) {
			super.onPostExecute(result);
			MyAdapter adapter = new MyAdapter(result, PullParserActivity.this);
			mProgress.dismiss();
			list.setAdapter(adapter);
		}
		
		public ArrayList<Item> parsingXml(String xml){
		ArrayList<Item> arItem = new ArrayList<Item>();
		Item itemClass = null;
		boolean initem = false;
		
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			
			parser.setInput(new StringReader(xml));
			
			int eventType = parser.getEventType();
			
			while(eventType != XmlPullParser.END_DOCUMENT){
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
				//	Toast.makeText(PullParserActivity.this, "Start Document", 1).show();
					break;
				case XmlPullParser.END_DOCUMENT:
				//	Toast.makeText(PullParserActivity.this, "End Document", 1).show();
					break;
				case XmlPullParser.START_TAG:
					if(parser.getName().equalsIgnoreCase("item")){
						initem = true;
						itemClass = new Item();
					}
					for(int i = 0; i<parser.getAttributeCount(); i++){
						if(parser.getAttributeName(i).equalsIgnoreCase("price")){
							itemClass.setItemPrice(Integer.parseInt(parser.getAttributeValue(i)));
						}else{
							itemClass.setMakerName(parser.getAttributeValue(i));
						}
					}
				//	Toast.makeText(PullParserActivity.this, "Start Tag", 1).show();
					break;
				case XmlPullParser.END_TAG:
					if(parser.getName().equals("item")){
						arItem.add(itemClass);
					}
				//	Toast.makeText(PullParserActivity.this, "End Tag", 1).show();
					break;
				case XmlPullParser.TEXT:
					if(initem){
						itemClass.setItemName(parser.getText());
						initem = false;
					}
				  // Toast.makeText(PullParserActivity.this, "Text", 1).show();
					break;
			}
				eventType = parser.next();
		}
		}catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return arItem;
}
	}
}

